/**
 * 
 */
package database;

import java.sql.*;
import java.util.concurrent.Semaphore;

/**
 * @author Kevin Hanson
 * 
 */
public class Database {
	private static boolean initialized = false;
	private static Connection conn = null;
	final static String[] tables = { "calender", "note", "diary", "log", "finance", "account" };
	private final Semaphore sem = new Semaphore(1, true);

	public Database() {
		if (initialized == false) {
			try {
				init();
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
				ex.printStackTrace();
				System.exit(-1);
			}
		}
	}

	private void init() throws Exception {
		initialized = true;
		Class.forName("org.sqlite.JDBC");
		conn = DriverManager.getConnection("jdbc:sqlite:data.db");

		dbSanityCheck();
	}

	public static Connection getConnection() {
		return conn;
	}

	/**
	 * Do sanity check on database tables
	 */
	private void dbSanityCheck() {
		DatabaseMetaData meta = null;
		String name = null;
		ResultSet results = null;

		try {
			meta = conn.getMetaData();
			// results = meta.getColumns(null, null, name, null);
			results = meta.getTables(null, null, null, null);
			System.out.println(meta);
			while (results.next()) {
				System.out.println(results.getString("TABLE_NAME"));
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
}
